package compiler.IR;

import java.util.HashSet;

import compiler.PrettyPrinter;
import compiler.Exceptions.TypeCheckerException;

public class MJIfElse extends MJIf {

	private MJBlock elseblock;

	public MJIfElse(MJExpression condition, MJBlock thenblock, MJBlock elseblock) {
		super(condition, thenblock);
		this.elseblock = elseblock;
	}
	
	public MJBlock getElseblock() {
		return elseblock;
	}

	public void prettyPrint(PrettyPrinter prepri) {
		super.prettyPrint(prepri);
		prepri.println("else");
		this.elseblock.prettyPrint(prepri);
	}

	MJType typeCheck() throws TypeCheckerException {
		
		MJType thenblock = this.thenblock.typeCheck();
		MJType elseblock = this.elseblock.typeCheck();
		MJType con = this.condition.getType();
		
		
		if(!con.isBoolean())
			throw new TypeCheckerException(null);
		
		return MJType.getVoidType();
	}

	void variableInit(HashSet<MJVariable> initialized)
			throws TypeCheckerException {
		
		condition.variableInit(initialized);
		
		if(condition.equals(true)){
		this.thenblock.variableInit(initialized);
		}
		else{		
		this.elseblock.variableInit(initialized);
		}
	}

}
